﻿using System;
using System.ComponentModel;
using System.Threading;

namespace Glorysoft.EAP.EAPClient.Common
{
    public abstract class ModelBase : INotifyPropertyChanged
    {
        SynchronizationContext context;
        public ModelBase(SynchronizationContext _context)
        {
            context = _context;
            OnPropertyChanged = propertyName =>
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    context.Post(t => handler(this, new PropertyChangedEventArgs((string)t)), propertyName);
                }
            };
        }
        public ModelBase()
        {
            OnPropertyChanged = propertyName =>
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            };
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected Action<string> OnPropertyChanged;
    }
}
